﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace GH.Web.BasePage
{
    public class PageBase : System.Web.UI.Page
    {

        /// <summary>
        /// 用户编号
        /// </summary>
        private int _UserID;

        public int UserID
        {
            get { return _UserID; }
            set { _UserID = value; }
        }


        /// <summary>
        /// 用户名称
        /// </summary>
        private string _UserName;

        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        /// <summary>
        /// 当前登陆状态
        /// </summary>
        private bool _IsLogin;

        /// <summary>
        /// 当前登陆状态
        /// </summary>
        public bool IsLogin
        {
            get { return _IsLogin; }
            set { _IsLogin = value; }
        }

        /// <summary>
        /// 页面名称
        /// </summary>
        private string _PageName;

        public string PageName
        {
            get { return _PageName; }
            set { _PageName = value; }
        }

        /// <summary>
        /// LOAD事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManagePage_Load(object sender, EventArgs e)
        {
            CheckAdminAuthority();
            if (!IsLogin)
            {
                ShowMessage();
            }
        }
        /// <summary>
        /// LOAD事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManagePage_LoadComplete(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public PageBase()
        {
            this.Load += new EventHandler(ManagePage_Load);
            this.LoadComplete += new EventHandler(ManagePage_LoadComplete);
        }

        private void CheckAdminAuthority()
        {
            IsLogin = true;
            try
            {
                if (Session["UserName"] == null
                    || "" == Session["UserName"].ToString()
                    || Session["UserID"] == null
                    || "" == Session["UserID"].ToString()
                    || Session["Union"] == null
                    || "" == Session["Union"].ToString()
                    || Session["Union"] == null
                    || "" == Session["SelectCurrentYear"].ToString()
                    || Session["UnionSelect"] == null
                    || "" == Session["UnionSelect"].ToString()
                    )
                {

                    IsLogin = false;
                }
            }
            catch
            {

                IsLogin = false;
            }
            if (IsLogin)
            {
                UserName = Session["UserName"].ToString();
                UserID = Convert.ToInt32(Session["UserID"].ToString());

            }
        }


        public void ShowMessage()
        {
            //System.Web.HttpContext.Current.Response.Clear();
            //System.Web.HttpContext.Current.Response.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head><title>");

            //System.Web.HttpContext.Current.Response.Write("错误提示");
            //System.Web.HttpContext.Current.Response.Write("</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
            //System.Web.HttpContext.Current.Response.Write("<script>setTimeout(\"window.top.location='/admin/login.aspx'\",3000)</script> ");
            //System.Web.HttpContext.Current.Response.Write("<style type=\"text/css\"><!-- body { margin: 20px; font-family: Tahoma, Verdana; font-size: 14px; color: #333333; background-color: #FFFFFF; }a {color: #1F4881;text-decoration: none;}--></style></head><body><div style=\"border: #cccccc solid 1px; padding: 20px; width: 500px; margin:auto\" align=\"center\">");

            //System.Web.HttpContext.Current.Response.Write("<div style=\"border: 0px; padding: 0px; width: 500px; margin:auto\"><strong>当前服务器时间:</strong> ");
            //System.Web.HttpContext.Current.Response.Write(System.DateTime.Now.ToShortTimeString());
            //System.Web.HttpContext.Current.Response.Write("<br /><strong>3秒钟后自动跳转，如不能跳转请手动</strong> ");
            //System.Web.HttpContext.Current.Response.Write("<br /><strong>选择操作:</strong> ");


            //System.Web.HttpContext.Current.Response.Write("<a href=\"/login.aspx\" target=\"_top\">登录</a> | <a href=\"JavaScript:window.close()\">退出</a>");

            //System.Web.HttpContext.Current.Response.Write("</div></div></body></html>");
            //System.Web.HttpContext.Current.Response.End();
            JavascriptFun("window.top.location='/login.aspx'");
        }

        /// <summary>
        /// 执行一个JS语句
        /// </summary>
        /// <param name="sentence">要执行的语句</param>
        protected void ExecuteJs(string sentence)
        {
            Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
            Context.Response.Write(sentence);
            Context.Response.Write("</script>");
        }

        protected string GetYesNoName(object o)
        {
            if (o.ToString() == "0")
            {
                return "否";
            } return "是";
        }

        #region MyRegion

        /// <summary>
        /// 取得用户权限
        /// </summary>
        /// <param name="RightId"></param>
        /// <returns></returns>
        public bool GetRight(int RightId)
        {
            GH.BLL.UserRights BLLUserRights = new GH.BLL.UserRights();
            return BLLUserRights.Exists(UserID, RightId);
        }

        public void CheckUserLogin()
        {
            if (!Validate_Session())
            {
                AlertMessage("您没有登录系统或会话已过期");
            }
        }


        /// <summary>
        /// 检查用户信息会话是否超时
        /// </summary>
        /// <returns></returns>
        protected bool Validate_Session()
        {
            return !SessionData.IsTimeout();
        }

        public void AlertMessage(string msg)
        {
            string script = "<script>alert('" + msg + "')</script>";
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 弹出页面
        /// </summary>
        /// <param name="url"></param>
        public void OpenUrl(string url)
        {
            string script = string.Format("<script language='JavaScript'>window.open('" + "{0}" + "','" + "','');<", url);
            script += "/";
            script += "script>";

            ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), script, false);
        }

        /// <summary>
        /// 返回上一页
        /// </summary>
        public void JavascriptGoBack()
        {
            string script = "<script>history.go(-1);</script>";
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 执行JavaScript方法
        /// </summary>
        /// <param name="script"></param>
        public void JavascriptFun(string script)
        {
            string AllScript = string.Format("<script>{0}</script>", script);
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), AllScript);
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        public void JavascriptClose()
        {
            string script = "<script>window.opener = null;window.close();</script>";
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 跳转到其他页面
        /// </summary>
        /// <param name="url">页面地址</param>7
        public void JavascriptGoUrl(string url)
        {
            string script = string.Format("<script>location = '{0}';</script>", url);
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 显示信息并跳转页面
        /// </summary>
        /// <param name="message"></param>
        /// <param name="url"></param>
        public void ShowMessageAndGoUrl(string message, string url)
        {
            string script = string.Format("<script>alert('{0}');location.href='{1}';</script>", message, url);
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }


        /// <summary>
        /// 获取MD5值
        /// </summary>
        /// <param name="data">原字符串</param>
        /// <returns>通过MD5加密后的</returns>
        public string GetMd5Code(string data)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(data, "md5").ToUpper();
        }


        /// <summary>
        /// 返回上一页
        /// </summary>
        protected bool GoBack()
        {
            if (ViewState["BackUrl"] != null)
            {
                Response.Redirect(ViewState["BackUrl"].ToString());
                return true;
            }
            else
                return false;
        }

        public static string CutString(string str, int maxlength)
        {
            if (str.Length > maxlength)
                return str.Substring(0, maxlength) + "....";
            else return str;
        }


        public string CurrentPageName()
        {
            string[] strArray = this.Request.Url.AbsoluteUri.Split('/');
            string[] strRealUrl = strArray[4].ToString().Split('?');
            return strRealUrl[0].ToString();
        }


        #endregion

    }
}
